// Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! This is Ferrium package manager!

use clap::{command, Parser};
use indicatif::MultiProgress;
use indicatif_log_bridge::LogWrapper;
use ferrium::{Commands, FerriumCommand};

#[derive(Parser)]
#[command(version, about)]
/// Ferrium package manager
struct Args {
    #[command(subcommand)]
    command: Commands,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    if std::env::var("RUST_LOG").is_err() {
        #[cfg(debug_assertions)]
        std::env::set_var("RUST_LOG", "debug");
        #[cfg(not(debug_assertions))]
        std::env::set_var("RUST_LOG", "info");
    }

    let logger = env_logger::builder()
        .format_timestamp(None)
        .format_module_path(false)
        .format_target(false)
        .build();
    let level = logger.filter();
    let multi_progress = MultiProgress::new();
    LogWrapper::new(multi_progress.clone(), logger).try_init()?;
    log::set_max_level(level);

    let args = Args::try_parse()?;

    match args.command {
        Commands::ListAvailable(command) => command.run(multi_progress).await?,
        Commands::ListInstalled(command) => command.run(multi_progress).await?,
        Commands::ListDeployed(command) => command.run(multi_progress).await?,
        Commands::DumpPkgStat(command) => command.run(multi_progress).await?,
        Commands::DumpDeployStat(command) => command.run(multi_progress).await?,
        Commands::ShowHash(command) => command.run(multi_progress).await?,
        Commands::Init(command) => command.run(multi_progress).await?,
        Commands::Chroot(command) => command.run(multi_progress).await?,
        Commands::Install(command) => command.run(multi_progress).await?,
        Commands::Uninstall(command) => command.run(multi_progress).await?,
        Commands::Cleanup(commands) => commands.run(multi_progress).await?,
        Commands::Deploy(command) => command.run(multi_progress).await?,
        Commands::Undeploy(command) => command.run(multi_progress).await?,
        Commands::Rollback(command) => command.run(multi_progress).await?,
    }

    Ok(())
}
